package com.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBAccess.DBAccess;

/**
 * Servlet implementation class MenuController
 */
/**
 * @author 661611
 * 
 */
public class MenuController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MenuController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if ("Admin".equals(request.getSession().getAttribute("userRole"))) {
			if ("AddWorker".equals(request.getParameter("action"))) {
				try {
					if ((DBAccess.query("Select * from Employee")) != null) {
						request.setAttribute("WorkerList",
								DBAccess.query("Select * from Employee"));
					} else {
						request.setAttribute("WorkerList", null);
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("whereTo", "viewWorker.jsp");
				request.getRequestDispatcher("/JSP/welcome.jsp").forward(
						request, response);
			} else if ("ViewWorker".equals(request.getParameter("action"))) {
				try {
					if ((DBAccess.query("Select * from Employee")) != null) {
						request.setAttribute("WorkerList",
								DBAccess.query("Select * from Employee"));
					} else {
						request.setAttribute("WorkerList", null);
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				request.setAttribute("whereTo", "viewWorker.jsp");
				request.getRequestDispatcher("/JSP/welcome.jsp").forward(
						request, response);
			} else if ("UpdateStudent".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/updateStudent.jsp").forward(
						request, response);
			} else if ("DeleteStudent".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/deleteStudent.jsp").forward(
						request, response);
			} else if ("CreateSchool".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/createSchool.jsp").forward(
						request, response);
			} else if ("ViewSchool".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/viewSchool.jsp").forward(
						request, response);
			} else if ("UpdateSchool".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/updateSchool.jsp").forward(
						request, response);
			} else if ("DeleteSchool".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/deleteSchool.jsp").forward(
						request, response);
			} else if ("CreateAdmin".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/createAdmin.jsp").forward(
						request, response);
			} else if ("ViewAdmin".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/viewAdmin.jsp").forward(
						request, response);
			} else if ("UpdateAdmin".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/updateAdmin.jsp").forward(
						request, response);
			} else if ("DeleteAdmin".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/deleteAdmin.jsp").forward(
						request, response);
			} else if ("ChangePassword".equals(request.getParameter("action"))) {
				request.getRequestDispatcher("/JSP/changePassword.jsp")
						.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
